(*
Copyright (c) 2014 Gilles Arcas-Luque (gilles dot arcas at gmail dot com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*)

unit URichEditEx;

interface

uses
  Windows, Messages, TntComCtrls, StrUtils;

type
  TRichEditEx = class(TTntRichEdit)
  public
    function GetFirstVisibleLine : integer;
    function GetCurrentLine : integer;
    procedure GotoLine(i : integer; j : integer = 0);
    procedure GotoFirstLine;
    procedure GotoLineRel(delta : integer);
    function GetLineFromChar(n : integer) : integer;
    function GetLineIndex(line : integer) : integer;
  end;

implementation

(*
//For some reason, the standard messages do not give the correct result
//here (wide string ?).

function TRichEditEx.GetLineFromChar(n : integer) : integer;
begin
  Result := Perform(EM_LINEFROMCHAR, n, 0)
end;

function TRichEditEx.GetLineIndex(line : integer) : integer;
begin
  Result := Perform(EM_LINEINDEX, 0, 0)
end;
*)

function TRichEditEx.GetLineFromChar(n : integer) : integer;
var
  p, i : integer;
begin
  p := 0;
  for i := 0 to Lines.Count - 1 do
    begin
      inc(p, Length(Lines[i]) + 2);
      if n < p then
        begin
          Result := i;
          exit
        end
    end;
  Result := -1
end;

function TRichEditEx.GetLineIndex(line : integer) : integer;
var
  i : integer;
begin
  Result := 0;
  for i := 0 to line  - 1do
    inc(Result, Length(Lines[i]) + 2)
end;

function TRichEditEx.GetFirstVisibleLine : integer;
begin
  Result := SendMessage(Handle, EM_GETFIRSTVISIBLELINE, 0, 0)
end;

function TRichEditEx.GetCurrentLine : integer;
begin
  Result := GetLineFromChar(SelStart)
end;

procedure TRichEditEx.GotoFirstLine;
begin
  SelStart := 0;
  Perform(EM_SCROLLCARET, 0, 0)
end;

procedure TRichEditEx.GotoLine(i : integer; j : integer = 0);
begin
  SelStart := GetLineIndex(i) + j;
  Perform(EM_SCROLLCARET, 0, 0)
end;

procedure TRichEditEx.GotoLineRel(delta : integer);
begin
  SendMessage(Handle, EM_LINESCROLL, 0, delta)
end;

end.
